package com.dz.lm.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.util.FileUploadUtil;
import com.dz.lm.common.utils.security.RSAUtil;

/**
 * 功能：存放控制器常用功能，为业务功能提供公共接口方法
 * 
 * @author zhanghairong
 * @since 2019-05-28
 */
public abstract class BasicControl {
	public static String RSAPUBLICKEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrC2970p5XeYp7E6Ngusd3+iUtEt9D84a8esZNm/fQP7ftdpr5dLrcMVc18YjtJ9gGppb5ezlGl5497PDWHRMcHWE7BeOp8+JmYJPp3LYO4k2yE+1lDxxCzFVRQ73A/Z81TwEsyb9BTQDu/FJhhr/nKa0NO50D9S8VO5ItcPSESwIDAQAB";
	public static String RSAPRIVATEKEY = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAKsLb3vSnld5insTo2C6x3f6JS0S30Pzhrx6xk2b99A/t+12mvl0utwxVzXxiO0n2Aamlvl7OUaXnj3s8NYdExwdYTsF46nz4mZgk+nctg7iTbIT7WUPHELMVVFDvcD9nzVPASzJv0FNAO78UmGGv+cprQ07nQP1LxU7ki1w9IRLAgMBAAECgYEAo5yXsmjgn6oaJeq+EAv6O7CimQABYv1yd541TA2SObiJlnMqgM4A5BmqY2Y8ZSYExS/bIJ+XRQhJDNE1+onrORjmvYUuUjxeRJq8AmIc5wdmBUrJ8+lSNfDhumIA4BCgDMci8xV8GacBqDd6Nh/TihXtPau/Xp5JP+hneXGOuSECQQDtuB0G+yKlOXLRyjkyzZRVw7aT8HAaDGHNF1pmgaZH4GbdCsDFEuVaIEpVkqC8YbLFRviNIsgxx5FLKG+icARbAkEAuDK58D2QliquUceZSdOp7BRzvmwLr42/ihAi8biuDutoCvMTl9sAiBz/pZVcwdnSO0d1iaJ1wr5L1+nmNtzC0QJBAMSboM5USKw0XmVqhnIhG0zvqIKkumKg2QaiCO98zUTnMMwih5q27R/kUvaBZhVIDG4jK5C3MrIRJ3bsw5+qW78CQHqBaj7Ig1gMTOGhEWcTeijENXKJI4B/kss9DjCbtk+/6vOVVQujIQhl2PUEkKtwtiysyxRd01pQ+9o++ti0HHECQQDBDLuz/7b6DT2f8ueYqgfr+FGB32rXyVSYrT8MeLHd7M7wFlzZkhNAkgOz6AQiE+kPecjiM9QsphxXhcI2tWQa";
	public Logger logger = LogManager.getLogger();
	public static final String RETCODE = "retCode";
	public static final String RETMSG = "retMsg";
	public static final String RESULT = "result";
	public static final String MESSAGE = "message";
	public static final String SUCCESS_MSG = "SUCCESS";
	public static final String SUCCESS_CODE = "000000";
	
	
	/**
	 * 返回值加密
	 * 
	 **/
	protected Map<String, Object> parameterEncrypt(HttpServletRequest request, JSONObject subjo) {
		logger.info("Return value of Process:" + subjo);
		if (subjo == null) {
			subjo = new JSONObject();
		}
		Map<String, Object> data = new HashMap<>();
		if (request.getAttribute("reqKey") != null && !"".equals(request.getAttribute("reqKey")) && !"null".equals(request.getAttribute("reqKey"))) {
			data.put("_resData", RSAUtil.encryptBy3Des(request.getAttribute("reqKey").toString(), subjo.toString()));
		} else {
			return subjo;
		}
		Map<String, Object> map = new HashMap<>();
		map.put(RETCODE, SUCCESS_CODE);
		map.put(RETMSG, SUCCESS_MSG);
		map.put("retData", data);
		return map;
	}
	
	/**
	 * 修改图片，图片位置变换的处理办法
	 * @param imgName 图片名称前缀
	 * @param param 参数列表
	 * @param maxImgCnt 最大图片数量
	 **/
	protected void checkImage(String imgName, Map<String, Object> param, int maxImgCnt) {
		for (int cnt = 1; cnt <= maxImgCnt; cnt++) {
			if (param.get(imgName + cnt) == null) {
				param.put(imgName + cnt, "");
			}
		}
	}
	
	/**
	 * 删除无效图片
	 * @param deleteFiles 图片url结合，用逗号分隔
	 **/
	protected void deleteImages(Object deleteFiles) {
		String files[] = null;
		boolean deleteFlag = false;
		if (deleteFiles != null && !"".equals(deleteFiles.toString())) {
			files = deleteFiles.toString().split(",");
			for (String file : files) {
				deleteFlag = FileUploadUtil.deleteFile(file);
				if (deleteFlag) {
					logger.info(file + "File's delete is Success!");
				} else {
					logger.info(file + "File's delete is Fail!");
				}
			}
		}
	}
}
